package ${package.Controller};

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.object.utils.Q;

import javax.annotation.Resource;

import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;
import java.net.URLEncoder;
import com.alibaba.excel.EasyExcel;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import ${package.Service}.${table.serviceName};
import ${package.Entity}.${entity};


/**
 * ${table.comment}
 *
 * @author ${author}
 **/
@RestController
@RequestMapping("<#if package.ModuleName?? && package.ModuleName != "">/${package.ModuleName}</#if>/<#if controllerMappingHyphenStyle>${entity}<#else>${entity}</#if>")
@Api(tags = "${table.comment}接口")
public class ${table.controllerName}{
    @Resource
    private ${table.serviceName} service;


    @ApiOperation("列表")
    @GetMapping("/list")
    public Q<List<${entity}>> list(@RequestParam Map<String, Object> params) {
        return service.listPage(params, true);
    }


    @ApiOperation("详情")
    @GetMapping("/info/{id}")
    public Q<${entity}> info(@PathVariable("id") String id) {
    ${entity} bean = service.getById(id);
        return new Q<${entity} >().ok(bean);
    }


    @ApiOperation("保存")
    @PostMapping("/save")
    public Q<Boolean> save(@RequestBody ${entity} bean) {
        return new Q<Boolean>().ok(service.save(bean));
    }

    @ApiOperation("修改根据id")
    @PostMapping("/update")
    public Q<Boolean> update(@RequestBody ${entity} bean) {
        return new Q<Boolean>().ok(service.updateById(bean));
    }

    @ApiOperation("批量操作")
    @PostMapping("/batchWord")
    public Q<Boolean> update(@RequestBody List<${entity}> beans) {
        return new Q<Boolean>().ok(service.updateBatchById(beans));
    }

    @ApiOperation("删除根据id")
    @DeleteMapping("/delete")
    public Q<Boolean> delete(@RequestBody String[] ids) {
        return new Q<Boolean>().ok(service.removeByIds(Arrays.asList(ids)));
    }

    @ApiOperation("导出")
    @GetMapping("export")
    public void importExcel(@ApiParam(value = "导出条件", required = true) @RequestParam(required = false) Map<String, Object> params, HttpServletResponse response) throws IOException {
        Q<List<${entity}>> list = service.listPage(params, false);
        // 设置响应头
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet; charset=UTF-8");
        response.setCharacterEncoding("UTF-8");
        String fileName = URLEncoder.encode("${table.comment}.xlsx", "UTF-8");
        response.setHeader("Content-Disposition", "attachment; filename=" + fileName);
        EasyExcel.write(response.getOutputStream(), ${entity}.class).sheet("${table.comment}").doWrite(list.getT());
    }

}